package com.zaofan.mobile.controller;

import com.zaofan.bean.Address;
import com.zaofan.bean.OpenBuilder;
import com.zaofan.exception.ServiceException;
import com.zaofan.mobile.bean.HotelInfo;
import com.zaofan.service.AccountService;
import com.zaofan.service.AddressService;
import com.zaofan.service.HotelService;
import com.zaofan.service.OpenBuilderService;
import com.zaofan.utils.JsonUtils;
import com.zaofan.utils.WebUtils;

import java.util.List;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Created by hxdn on 2017/3/8.
 */
@WebServlet("/hotel")
public class HotelController  extends BaseServlet {

    private HotelService hotelService = new HotelService();;
    private OpenBuilderService mOpenBuilderService = new OpenBuilderService();
    private AddressService addressService = new AddressService();
    private AccountService mAccountService = new AccountService();



    public void getHotel(HttpServletRequest request,HttpServletResponse response){
        String hotelNo = request.getParameter("hotelNo");
        if(hotelNo==null||"".equals(hotelNo)){
            JsonUtils.writeFailJson("酒店编号不能为空失败", response);
        }
        else {
            HotelInfo hotelInfo = hotelService.findOpenHotelByHotelNo(hotelNo);
            OpenBuilder openBuilder = mOpenBuilderService.findById(hotelInfo.getAddressId());
            JsonUtils.writeSuccessJson("成功",openBuilder,response);
        }
    }
    public void addAddress(HttpServletRequest request,HttpServletResponse response) throws ServiceException {
        Address address = WebUtils.write2bean(request, Address.class);

            System.out.println(address.toString());
            addressService.addAddress(address);
            List<Address> address1 = addressService.findByAccountId(address.getAccountId());
            JsonUtils.writeSuccessJson("添加成功",address1.get(0),response);
    }


    public void registerTourists(HttpServletRequest request,HttpServletResponse response) throws Exception{

        String openid = request.getParameter("mobile");
        if(openid==null||"".equals(openid)){
            JsonUtils.writeFailJson("账户信息不能为空",response);
            return;
        }
        com.zaofan.bean.Account account = WebUtils.write2bean(request, com.zaofan.bean.Account.class);
        account.setPlatform("WechatApp");
        com.zaofan.bean.Account temp = mAccountService.findByMobile(account.getMobile());
        if(temp!=null){
            JsonUtils.writeSuccessJson("已有账户",temp.getId(),response);
        }
        else {
            account.setPassword("无人知道");
            mAccountService.add(account);
            temp = mAccountService.findByMobile(account.getMobile());
            JsonUtils.writeSuccessJson("添加成功",temp.getId(),response);
        }

    }

}
